import { app, BrowserWindow, ipcMain, Tray, Menu,globalShortcut } from "electron";
import path from "path";
import fs from 'fs'
import {createWindowMain,childWindw,dragWindw} from './windows'
import {devtool} from './loadDevelop'
//Number(window.localStorage.getItem('windowXX'))

process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true'

//添加一个信息文件
fs.appendFile(path.join(__dirname, '../message.txt'), '', (err) => {
  console.log(err);
})



app.whenReady().then( async () => {
  let child:BrowserWindow = await childWindw();
  let drag:BrowserWindow = await dragWindw();
  let winMain:BrowserWindow = await createWindowMain();
  devtool();
  app.on("activate", () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindowMain();
    }
  });

  //托盘事件
  let appIcon = new Tray(path.join(__dirname, '../icons/favicon.ico'))
  appIcon.on('double-click', () => {
    winMain.show()
  })
  const contextMenu = Menu.buildFromTemplate([
    {
      label: '退出', type: 'normal', click: () => {
        winMain.destroy();
      }
    },
    {
      label: '显示主页面', type: 'normal', click: () => {
        winMain.show();
      }
    }
  ])
  // Call this again for Linux because we modified the context menu
  appIcon.setContextMenu(contextMenu)
  //托盘事件结束



  //右键菜单
  ipcMain.on('show-context-menu', (event) => {
    const menu = Menu.buildFromTemplate([{
      label: '刷新', type: 'normal', role: 'reload'
    },
    {
      label: '开发者工具', type: 'normal', role: 'toggleDevTools'
    }
    ])
    menu.popup()
  })

  //隐藏app菜单
  Menu.setApplicationMenu(null)

  //获取当前窗口的宽高
  ipcMain.on('get-screen-X-Y', (e) => {
    e.reply('there-X-Y', { width: winMain.getBounds().width, height: winMain.getBounds().height })
  })

  winMain.on('closed',()=>{
    child.destroy();
    drag.destroy();
  })
});



app.on("window-all-closed", () => {
  if (process.platform !== "darwin") {
    app.quit();
  }
});

